
package jaxb;

//import es.sgi.spf.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * This class stores a reference to a File object to be read/written and provides
 * access to an inputstream and outputstream derived from it
*/
public class FileReaderWriter implements ReaderWriter {
    
    /**
     * The file this class represents
    */
    protected File file;  
    
    /**
     * The name of the file
     */
    private String filename;
    
    /**
     * The directory of the file
     */
    private String dir;    
    
    /**
     * Creates a new instance of FileReaderWriter
     * @param dir The directory the file resides
     * @param filename The name of the file to be read/written
    */  
    public FileReaderWriter(File dir, String filename){
        new File(dir, filename);
        this.dir = dir.getParent();
        this.filename = filename;
    }
    
    /**
     * Creates a new instance of FileReaderWriter
     * @param filename The full file name of the file to be read/written
    */
    public FileReaderWriter(String filename){    
        this.filename = new File(filename).getName();
        this.dir = new File(filename).getParent();
    }
    
    /**
     * Creates a new instance of FileReaderWriter
     * @param file The file object to be read/written
    */
    public FileReaderWriter(File file){    
        //new File(file.getAbsolutePath());
        this.filename = file.getName();
        this.dir = file.getParent();
    }
    
    /**
     * Returns an inputstream created from the File object
     * @return The inputstream
    */
    public InputStream getInputStream(){
        try {
          return new FileInputStream(new File(this.dir, this.filename));
        } catch (FileNotFoundException ex){}
        return null;
    }
    
    /**
     * Returns an outputstream created from the File object
     * @return The outputstream
    */
    public OutputStream getOutputStream(){
        try {
          return new FileOutputStream(new File(this.dir, this.filename));
        } catch (FileNotFoundException ex) {}
        return null;
    }
      
    /**
     * Returns the last modified date in milliseconds since 1/1/1970
     * @return The last modified milliseconds
    */
    public long getLastModifiedMillis(){
        return new File(dir, filename).lastModified();
    }
    
    /**
     * Returns the file of the FileReaderWriter
     * @return File The file of the FileReaderWriter
     */
    public File getFile(){
        return new File(dir, filename);
    }
    
    /**
     * Returns a string representation of this object
     * @return String The string representation of this object
    */
    public String toString() {
        return "File path:"+filename;
    }

    /**
     * Returns the name of the file
     * @return String The name of the file
     */
    public String getFilename() {
        return filename;
    }

    /**
     * Returns the directory of the file
     * @return String The directory of the fil
     */
    public String getDir() {
        return dir;
    }
}